Release 10.1A: OpenEdge Development:
Progress 4GL Handbook


REPEAT blocks

There’s a third kind of iterating block that is in between DO and FOR in its effects, the REPEAT block. It supports just about all the same syntax as a FOR block. You can add a FOR clause for one or more tables to it. You can use a WHILE phrase or the expression TO expression phrase. You can scope a frame to it.

A block that begins with the REPEAT keyword shares these default characteristics with a FOR block:

By contrast, it shares this important property with a DO block: It does not automatically read records as it iterates.

So what is a REPEAT block for? It is useful in cases where you need to process a set of records within a block but you need to navigate through the records yourself, rather than simply proceeding to the next record automatically on each iteration. The sample procedure starting in the "Index cursors" sectionshows you an example of where to use the REPEAT block.

One of the common ways to use a REPEAT block in older character applications is to repeat a data entry action until the user hits the ESCAPE key to end. Here’s a very simple but powerful example:

REPEAT: 
    INSERT customer EXCEPT comments WITH 2 COLUMNS. 
END. 

You haven’t seen the INSERT statement before and you won’t see much of it again, even though it’s one of the most powerful statements in the language. Figure 6–14 shows what you get from that one simple statement.

Figure 6–14: Results of using the INSERT statement

It’s a complete data entry screen, complete with initial values for fields that have one. The field help displays at the bottom of the window. Inside a REPEAT loop, this lets you create one new Customer record after another until you’re done and you press ESCAPE.

Why won’t you see the INSERT statement again? Because INSERT is one of those statements that mixes up all aspects of Progress, from the user interface to saving the data off into the database. And in a modern GUI application, you need to separate out all those things into separate parts of your application. You’ll look at database updates in Chapter 16, "Updating Your Database and Writing Triggers," so there’s no need to go into this example any further beyond understanding what the REPEAT block does around the statement.

Using the PRESELECT keyword to get data in advance

One typical use of the REPEAT block that is still valuable is when you use it with a construct called a PRESELECT. To understand this usage, you need to think a little about what happens when an iterating block like a FOR EACH block begins. Progress evaluates the record retrieval the FOR EACH statement defines. It then goes out to the database and retrieves the first record of the set of related records that satisfies the statement and makes the record available to the block. When the block iterates, Progress goes and gets the next record. As long as it’s possible to identify what the first and the next records are by using one or more indexes, Progress doesn’t bother reading all the records in advance. It just goes out and gets them when the block needs them.

If you specify a BY clause that requires a search of the database that an index can’t satisfy, Progress has no choice but to retrieve all the records in advance and build up a list in sort order. But this is not ordinarily the case. It’s much more efficient simply to get the records when you need them.

Sometimes, though, you need to force Progress to get all the records that satisfy the FOR EACH statement in advance, even when the sort order itself doesn’t require it. For example, if it’s possible that you will modify an indexed field in some of the records in such a way that they would appear again later in the retrieval process, you need to make sure that the set of records you’re working with is predetermined. The PRESELECT keyword gives you this. It tells Progress to build up a list of pointers to all the records that satisfy the selection criteria before it starts iterating through the block. This assures you that each record is accessed only once.

In summary, a REPEAT block does everything a FOR block does, but it does not automatically advance to the next record as it iterates. You should use a REPEAT block in cases where you want to control the record navigation yourself, typically using the FIND statement described in the next section. It provides you with record, frame, and transaction scoping.

Because it provides all these services, a REPEAT block is relatively expensive compared to a DO block. Use the simpler DO block instead of a REPEAT block, unless you need the record-oriented services provided by the REPEAT block.


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095